###############################################
# === xenomorph_archaeology_events.txt ===
###############################################
###############################################
# Xenomorph Archaeology Events
# Pass 16: 5-stage Crashed Derelict digsite events.
###############################################

namespace = xeno_arch

# Stage 1 — Initial discovery: outer hull breached
fleet_event = {
	id = xeno_arch.1
	title = xeno_arch.1.name
	desc = xeno_arch.1.desc
	picture = GFX_evt_crashed_ship
	archaeology = yes
	is_triggered_only = yes

	option = {
		name = xeno_arch.1.a
		owner = { add_resource = { society_research = 50 } }
	}
}

# Stage 2 — Hull penetrated: alien engineering
fleet_event = {
	id = xeno_arch.2
	title = xeno_arch.2.name
	desc = xeno_arch.2.desc
	picture = GFX_evt_underground_excavation
	archaeology = yes
	is_triggered_only = yes

	option = {
		name = xeno_arch.2.a
		owner = { add_resource = { engineering_research = 100 } }
	}
}

# Stage 3 — Inner chambers reached
fleet_event = {
	id = xeno_arch.3
	title = xeno_arch.3.name
	desc = xeno_arch.3.desc
	picture = GFX_evt_alien_artifacts
	archaeology = yes
	is_triggered_only = yes

	option = {
		name = xeno_arch.3.a
		owner = { add_resource = { engineering_research = 150 } }
	}
}

# Stage 4 — Memory core extracted
fleet_event = {
	id = xeno_arch.4
	title = xeno_arch.4.name
	desc = xeno_arch.4.desc
	picture = GFX_evt_alien_artifacts
	archaeology = yes
	is_triggered_only = yes

	option = {
		name = xeno_arch.4.a
		add_resource = {
			society_research = 200
		}
		capital_scope = {
			add_modifier = { modifier = xenomorph_derelict_memory_core days = 90 }
		}
	}
}

# Stage 5 — Final recovery: derelict's full secrets
fleet_event = {
	id = xeno_arch.5
	title = xeno_arch.5.name
	desc = xeno_arch.5.desc
	picture = GFX_evt_alien_artifacts
	archaeology = yes
	is_triggered_only = yes

	option = {
		name = xeno_arch.5.a
		add_resource = {
			engineering_research = 500
			alloys = 500
		}
		add_modifier = { modifier = xenomorph_derelict_salvage days = 3650 }
	}
}

###############################################
# === xenomorph_origin_events.txt ===
###############################################
###############################################
# Xenomorph Origin Events
#
# Contains events tied to specific Xenomorph origins:
# - Queen Mother origin startup and maintenance
# - Xenomorph Prime origin startup
# Duplicate Queen Mother start file removed.
###############################################

###############################
### Queen Mother Events     ###
###############################

namespace = xeno_queen_mother

# Maintenance event for Queen Mother / Progenitor-style planets.
# Call this from an annual or monthly on_action, or from your existing maintenance pulse.
country_event = {
	id = xeno_queen_mother.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_xenomorph_queen_mother
	}

	immediate = {
		every_owned_planet = {
			limit = {
				is_colony = yes
				NOT = { xeno_has_planetary_queen_presence = yes }
			}
			add_modifier = {
				modifier = xenomorph_no_planetary_queen_penalty
				days = 370
			}
		}

		every_owned_planet = {
			limit = {
				is_colony = yes
				xeno_has_planetary_queen_presence = yes
			}
			remove_modifier = xenomorph_no_planetary_queen_penalty
		}
	}
}

namespace = xenomorph_queen_mother_start

country_event = {
	id = xenomorph_queen_mother_start.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_xenomorph_queen_mother
	}

	immediate = {
		ruler = {
			if = {
				limit = { NOT = { has_trait = leader_trait_xenomorph_queen_mother_immortal } }
				add_trait = leader_trait_xenomorph_queen_mother_immortal
			}
		}

		capital_scope = {
			set_planet_class = pc_xenomorph_hive
			set_planet_flag = xenomorph_hive_world
			if = {
				limit = { NOT = { has_modifier = xenomorph_hive_world } }
				add_modifier = { modifier = xenomorph_hive_world days = -1 }
			}
			set_planet_flag = xenomorph_allow_queen_mother_hive_setup

			# Queen Mother starts must not retain vanilla capital buildings.
			remove_building = building_colony_shelter
			remove_building = building_capital
			remove_building = building_major_capital
			remove_building = building_system_capital
			remove_building = building_hive_capital
			remove_building = building_hive_major_capital
			remove_building = building_machine_capital
			remove_building = building_machine_major_capital
			remove_building = building_machine_system_capital

			# Replace vanilla Hive starting infrastructure with Xenomorph equivalents.
			remove_building = building_spawning_pool
			remove_building = building_foundry_1
			remove_building = building_hydroponics_farm
			remove_building = building_research_lab_1

			# Pre-built spawning-pool replacement + warren/housing replacement (game start).
			if = { limit = { NOT = { has_building = building_xenomorph_queen_mother_royal_jelly_pods } } add_building = building_xenomorph_queen_mother_royal_jelly_pods }
			if = { limit = { NOT = { has_building = building_xenomorph_resin_spire } } add_building = building_xenomorph_resin_spire }

			# === Capital chain: Queen Mother Hive ===
			if = {
				limit = { NOT = { has_building = building_xenomorph_queen_mother_hive } }
				add_building = building_xenomorph_queen_mother_hive
			}

			# === Royal Jelly chain (QM-specific Royal Jelly Pods) ===
			# Use the QM-specific T1 royal jelly building if it exists, otherwise the generic T1.


			# === Food / biomass ===

			# === Unity / synapse ===

			# === T1 amenity (Resin Spire) ===

			# === T1 unity-secondary (Pheromone Node) ===

			# === T1 defense (Drone's Nest) ===

			# === Civic-gated manufacturing T1 ===

			clr_planet_flag = xenomorph_allow_queen_mother_hive_setup
		}
	}
}

namespace = xenogeneral

###############################################
# xenogeneral.4 — Queen Mother Origin flavour setup
# Fires at game start for empires with origin_xenomorph_queen_mother.
# Building replacement is handled by xenomorph_queen_mother_start.1.
###############################################

country_event = {
	id = xenogeneral.4
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_origin = origin_xenomorph_queen_mother
	}

	# Note: pc_hive class change, hive_world flag/modifier, and immortal ruler trait
	# are handled by xenomorph_queen_mother_start.1. This event only handles the
	# bonus pop spawn and throne-world flavour flag.
	immediate = {
		capital_scope = {
			# Spawn 5 additional Xenomorph pops to support the royal throne-world start.
			create_pop = {
				species = owner_main_species
				ethos = ethic_gestalt_consciousness
			}
			create_pop = {
				species = owner_main_species
				ethos = ethic_gestalt_consciousness
			}
			create_pop = {
				species = owner_main_species
				ethos = ethic_gestalt_consciousness
			}
			create_pop = {
				species = owner_main_species
				ethos = ethic_gestalt_consciousness
			}
			create_pop = {
				species = owner_main_species
				ethos = ethic_gestalt_consciousness
			}

			# Set the capital planet flag for flavour event triggers.
			set_planet_flag = queen_mother_throne_world
		}
	}
}

###############################################
# xenogeneral.6 — Xenomorph Prime Origin capital setup
# Gives the origin its promised Hive World / royal start.
###############################################

country_event = {
	id = xenogeneral.6
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_origin = origin_xenomorph_prime
	}

	immediate = {
		capital_scope = {
			set_planet_class = pc_xenomorph_hive
			set_planet_flag = xenomorph_hive_world
			if = {
				limit = { NOT = { has_modifier = xenomorph_hive_world } }
				add_modifier = { modifier = xenomorph_hive_world days = -1 }
			}
			set_planet_flag = xenomorph_prime_throne_world

			# Strip vanilla capital buildings.
			if = { limit = { has_building = building_colony_shelter } remove_building = building_colony_shelter }
			if = { limit = { has_building = building_capital } remove_building = building_capital }
			if = { limit = { has_building = building_major_capital } remove_building = building_major_capital }
			if = { limit = { has_building = building_system_capital } remove_building = building_system_capital }
			if = { limit = { has_building = building_hive_capital } remove_building = building_hive_capital }
			if = { limit = { has_building = building_hive_major_capital } remove_building = building_hive_major_capital }

			# Xenomorph Prime starts with Empress Hive (T4 — origin spec says "starts with one Empress ruler").
			# Pass 13 fix: removed Brood Queen Hive add — both capitals are capital=yes/base_cap_amount=1
			# and cannot coexist on the same planet; the second add was silently overwriting the first.
			if = { limit = { NOT = { has_building = building_xenomorph_empress_hive } } add_building = building_xenomorph_empress_hive }

			# Strip vanilla support buildings.
			if = { limit = { has_building = building_spawning_pool } remove_building = building_spawning_pool }
			if = { limit = { has_building = building_foundry_1 } remove_building = building_foundry_1 }
			if = { limit = { has_building = building_hydroponics_farm } remove_building = building_hydroponics_farm }
			if = { limit = { has_building = building_research_lab_1 } remove_building = building_research_lab_1 }

			# Pre-built spawning-pool replacement + warren/housing replacement (game start).
			if = { limit = { NOT = { has_building = building_xenomorph_royal_jelly_pods } } add_building = building_xenomorph_royal_jelly_pods }
			if = { limit = { NOT = { has_building = building_xenomorph_resin_spire } } add_building = building_xenomorph_resin_spire }

			# Royal Jelly chain T1.

			# Food.

			# Unity.


			# T1 amenity (Resin Spire).

			# T1 unity-secondary (Pheromone Node).

			# T1 defense-secondary (Drone's Nest).

			# Civic-gated manufacturing T1.
		}
	}
}


# FIX 2026-07: this event's id-prefix (xenomorph_lab_escape_start) was never
# declared as a namespace, so the whole origin-setup event failed to load and
# ran nothing (verified: LV/MGHG never define an event with an undeclared
# namespace; event-modding wiki requires a namespace line).
namespace = xenomorph_lab_escape_start

# =====================================================================
# Pass 16: Lab Escape origin setup. The player designs a secondary host
# species in the empire creator (see has_secondary_species block in
# common/governments/civics/xenomorph_origins.txt). At game start, that
# secondary species is enslaved as Host Stock and given a temporary
# containment-instability modifier.
# =====================================================================
country_event = {
	id = xenomorph_lab_escape_start.1
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_origin = origin_xenomorph_lab_escape
	}

	immediate = {
		capital_scope = {
			# Lab Escape capital is NOT pc_hive — the lab was on a normal world.
			# But flag it as a hive world so events still recognize it.
			set_planet_flag = xenomorph_hive_world
			if = {
				limit = { NOT = { has_modifier = xenomorph_hive_world } }
				add_modifier = { modifier = xenomorph_hive_world days = -1 }
			}
			set_planet_flag = xenomorph_lab_escape_origin

			# 10-year temporary containment instability modifier representing
			# the chaos of the lab outbreak still being contained.
			add_modifier = { modifier = xenomorph_lab_escape_containment days = 3650 }

			# Strip vanilla capital buildings.
			if = { limit = { has_building = building_colony_shelter } remove_building = building_colony_shelter }
			if = { limit = { has_building = building_capital } remove_building = building_capital }
			if = { limit = { has_building = building_major_capital } remove_building = building_major_capital }
			if = { limit = { has_building = building_system_capital } remove_building = building_system_capital }
			if = { limit = { has_building = building_hive_capital } remove_building = building_hive_capital }
			if = { limit = { has_building = building_hive_major_capital } remove_building = building_hive_major_capital }

			# Strip vanilla support buildings.
			if = { limit = { has_building = building_spawning_pool } remove_building = building_spawning_pool }
			if = { limit = { has_building = building_foundry_1 } remove_building = building_foundry_1 }
			if = { limit = { has_building = building_hydroponics_farm } remove_building = building_hydroponics_farm }
			if = { limit = { has_building = building_research_lab_1 } remove_building = building_research_lab_1 }

			# Pre-built spawning-pool replacement + warren/housing replacement (game start).
			if = { limit = { NOT = { has_building = building_xenomorph_royal_jelly_pods } } add_building = building_xenomorph_royal_jelly_pods }
			if = { limit = { NOT = { has_building = building_xenomorph_resin_spire } } add_building = building_xenomorph_resin_spire }

			# Add Xenomorph T1 capital chain.
			if = { limit = { NOT = { has_building = building_xenomorph_queens_nest_1 } } add_building = building_xenomorph_queens_nest_1 }
		}

		# Enslave the secondary species as Host Stock.
		# secondary_species references the player-designed host species.
		every_owned_species = {
			limit = {
				NOT = { has_trait = trait_xenomorph }
				is_sapient = yes
				NOR = {
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
				}
			}
			set_species_rights = {
				type = slavery_xenomorph_host_stock
				country = root
			}
		}

		# FIX 2026-07: secondary-species pop creation MOVED to
		# xenomorph_lab_escape_start.2 on the on_press_begin hook. At
		# on_game_start_country (start.1's hook) the designer secondary species
		# is not reliably owned yet, so random_owned_species found nothing here.
		# MGHG_game_start.28 and lv_game_start.3 both create secondary-species
		# pops on a later hook (on_press_begin / empire_init_capital_planet),
		# never on on_game_start_country.
	}
}


# =====================================================================
# FIX 2026-07: Laboratory Escapees host-pop creation.
# Fires from on_press_begin (country scope) where the designer-created
# secondary (host) species is reliably owned. Mirrors MGHG_game_start.28:
# random_owned_species filter -> event_target -> capital_scope create_pop_group.
# Also (re)applies Host Stock slavery rights on the found species so the
# spawned pops are enslaved regardless of start.1's earlier-hook timing.
# =====================================================================
# =====================================================================
# FIX 2026-07 (revised): Laboratory Escapees host-pop creation.
# Fires from empire_init_capital_planet (planet scope, capital). At this hook
# last_created_species is the designer-created secondary (host) species, so we
# do not depend on it already owning pops (the failure mode of the previous
# on_press_begin + random_owned_species version). Mirrors LV lv_game_start.3.
# [NEEDS IN-GAME VERIFICATION] If host pops still do not appear, the cause is
# upstream (vanilla may not seed a non-hive secondary for a hive-mind empire at
# all) and a different approach is required.
# =====================================================================
planet_event = {
	id = xenomorph_lab_escape_start.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner = { has_origin = origin_xenomorph_lab_escape }
	}

	immediate = {
		# Guard: only run once per empire even though the hook may fire per planet.
		if = {
			limit = { owner = { NOT = { has_country_flag = xeno_lab_escape_host_seeded } } }
			owner = { set_country_flag = xeno_lab_escape_host_seeded }

			last_created_species = {
				set_species_rights = {
					type = slavery_xenomorph_host_stock
					country = root.owner
				}
			}
			# BALANCE FLAG: size 2000 (x100 pop scale = ~20 pops) is a PLACEHOLDER
			# so host pops outnumber the starting Xenomorph pops per the origin
			# tooltip. Needs Crow's target number; tune in the balance pass.
			create_pop_group = {
				size = 2000
				species = last_created_species
			}
		}
	}
}


# FIX 2026-07: undeclared namespace (see Lab Escape note above) - origin setup
# never ran. Declaring it here so the crashed-derelict start event loads.
namespace = xenomorph_crashed_derelict_start

# =====================================================================
# Pass 16: Crashed Derelict origin setup. Capital becomes a hive world built
# atop the wreckage of an ancient Xenomorph starship. A 5-stage archaeology
# site is created on the capital — the player must send a science ship to
# excavate it for the derelict's salvage bonuses (see xeno_arch.1 through .5).
# =====================================================================
country_event = {
	id = xenomorph_crashed_derelict_start.1
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_origin = origin_xenomorph_crashed_derelict
	}

	immediate = {
		set_country_flag = xenomorph_crashed_derelict_site_owner

		capital_scope = {
			set_planet_class = pc_hive
			set_planet_flag = xenomorph_hive_world
			if = {
				limit = { NOT = { has_modifier = xenomorph_hive_world } }
				add_modifier = { modifier = xenomorph_hive_world days = -1 }
			}
			set_planet_flag = xenomorph_crashed_derelict_site

			# Strip vanilla capital buildings.
			if = { limit = { has_building = building_colony_shelter } remove_building = building_colony_shelter }
			if = { limit = { has_building = building_capital } remove_building = building_capital }
			if = { limit = { has_building = building_major_capital } remove_building = building_major_capital }
			if = { limit = { has_building = building_system_capital } remove_building = building_system_capital }
			if = { limit = { has_building = building_hive_capital } remove_building = building_hive_capital }
			if = { limit = { has_building = building_hive_major_capital } remove_building = building_hive_major_capital }

			# Strip vanilla support buildings.
			if = { limit = { has_building = building_spawning_pool } remove_building = building_spawning_pool }
			if = { limit = { has_building = building_foundry_1 } remove_building = building_foundry_1 }
			if = { limit = { has_building = building_hydroponics_farm } remove_building = building_hydroponics_farm }
			if = { limit = { has_building = building_research_lab_1 } remove_building = building_research_lab_1 }

			# Pre-built spawning-pool replacement + warren/housing replacement (game start).
			if = { limit = { NOT = { has_building = building_xenomorph_royal_jelly_pods } } add_building = building_xenomorph_royal_jelly_pods }
			if = { limit = { NOT = { has_building = building_xenomorph_resin_spire } } add_building = building_xenomorph_resin_spire }

			# Add T1 capital — single Queen, lean start.
			if = { limit = { NOT = { has_building = building_xenomorph_queens_nest_1 } } add_building = building_xenomorph_queens_nest_1 }

			# Minimal T1 chain — only Drone's Nest and Resin Spire.

			# Spawn the archaeology site.
			create_archaeological_site = xenomorph_crashed_derelict_digsite
		}
	}
}


# FIX 2026-07: undeclared namespace (see Lab Escape note above) - origin setup
# never ran. Declaring it here so the scattered-brood start event loads.
namespace = xenomorph_scattered_brood_start

# =====================================================================
# Pass 16: Scattered Brood origin setup. Sets up the player's home capital
# and spawns 3 independent AI Xenomorph empires in rim systems. Each starts
# neutral toward the player. Each gets a different Xeno civic for variety.
#
# DLC note: uses create_country with day_zero_contact (Federations DLC for
# clean contact handling; works without DLC but contact is rougher).
# =====================================================================
country_event = {
	id = xenomorph_scattered_brood_start.1
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_origin = origin_xenomorph_scattered_brood
	}

	immediate = {
		set_country_flag = xenomorph_scattered_brood_origin

		# Capital: pc_hive + T1 chain. Lean start; the player relies on
		# eventually consolidating with the scattered broods.
		capital_scope = {
			set_planet_class = pc_hive
			set_planet_flag = xenomorph_hive_world
			if = {
				limit = { NOT = { has_modifier = xenomorph_hive_world } }
				add_modifier = { modifier = xenomorph_hive_world days = -1 }
			}

			if = { limit = { has_building = building_colony_shelter } remove_building = building_colony_shelter }
			if = { limit = { has_building = building_capital } remove_building = building_capital }
			if = { limit = { has_building = building_major_capital } remove_building = building_major_capital }
			if = { limit = { has_building = building_system_capital } remove_building = building_system_capital }
			if = { limit = { has_building = building_hive_capital } remove_building = building_hive_capital }
			if = { limit = { has_building = building_hive_major_capital } remove_building = building_hive_major_capital }

			if = { limit = { has_building = building_spawning_pool } remove_building = building_spawning_pool }
			if = { limit = { has_building = building_foundry_1 } remove_building = building_foundry_1 }
			if = { limit = { has_building = building_hydroponics_farm } remove_building = building_hydroponics_farm }
			if = { limit = { has_building = building_research_lab_1 } remove_building = building_research_lab_1 }

			# Pre-built spawning-pool replacement + warren/housing replacement (game start).
			if = { limit = { NOT = { has_building = building_xenomorph_royal_jelly_pods } } add_building = building_xenomorph_royal_jelly_pods }
			if = { limit = { NOT = { has_building = building_xenomorph_resin_spire } } add_building = building_xenomorph_resin_spire }

			if = { limit = { NOT = { has_building = building_xenomorph_queens_nest_1 } } add_building = building_xenomorph_queens_nest_1 }
		}

		# Spawn 3 independent Xeno AI empires in rim systems.
		# Each gets a random Xeno civic.
		save_event_target_as = scattered_brood_root

		# Brood 1
		random_rim_system = {
			limit = {
				NOT = { is_same_value = root.capital_scope.solar_system }
			}
			spawn_system = {
				min_distance = 30
				max_distance = 70
				initializer = "xeno_scattered_brood_system"
			}
			last_created_system = {
				save_event_target_as = scattered_brood_system_1
				create_country = {
					name = random
					name_list = random
					species = root.owner_main_species
					type = default
					authority = auth_hive_mind
					civics = {
						civic = random
						civic = random
					}
					flags = { xenomorph_scattered_brood_npc }
					ignore_initial_colony_error = yes
					day_zero_contact = yes
					effect = {
						save_event_target_as = scattered_brood_npc_1
						set_country_flag = is_xenomorph_empire_npc
						establish_communications_no_message = root
					}
				}
			}
		}

		# Brood 2
		random_rim_system = {
			limit = {
				NOT = { is_same_value = root.capital_scope.solar_system }
				NOT = { is_same_value = event_target:scattered_brood_system_1 }
			}
			spawn_system = {
				min_distance = 30
				max_distance = 70
				initializer = "xeno_scattered_brood_system"
			}
			last_created_system = {
				save_event_target_as = scattered_brood_system_2
				create_country = {
					name = random
					name_list = random
					species = root.owner_main_species
					type = default
					authority = auth_hive_mind
					civics = {
						civic = random
						civic = random
					}
					flags = { xenomorph_scattered_brood_npc }
					ignore_initial_colony_error = yes
					day_zero_contact = yes
					effect = {
						save_event_target_as = scattered_brood_npc_2
						set_country_flag = is_xenomorph_empire_npc
						establish_communications_no_message = root
					}
				}
			}
		}

		# Brood 3
		random_rim_system = {
			limit = {
				NOT = { is_same_value = root.capital_scope.solar_system }
				NOT = { is_same_value = event_target:scattered_brood_system_1 }
				NOT = { is_same_value = event_target:scattered_brood_system_2 }
			}
			spawn_system = {
				min_distance = 30
				max_distance = 70
				initializer = "xeno_scattered_brood_system"
			}
			last_created_system = {
				create_country = {
					name = random
					name_list = random
					species = root.owner_main_species
					type = default
					authority = auth_hive_mind
					civics = {
						civic = random
						civic = random
					}
					flags = { xenomorph_scattered_brood_npc }
					ignore_initial_colony_error = yes
					day_zero_contact = yes
					effect = {
						set_country_flag = is_xenomorph_empire_npc
						establish_communications_no_message = root
					}
				}
			}
		}
	}
}

###############################################
# === xenomorph_planet_events.txt ===
###############################################
###############################################
# Xenomorph Hive World Events
# Put in: events/xenomorph_hive_world_events.txt
###############################################

namespace = xeno_hive_world

# Yearly maintenance. Applies owner-specific modifiers and keeps the old
# xenomorph_hive_world flag synced for older district/building/event checks.
country_event = {
	id = xeno_hive_world.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_planet = { xeno_is_xenomorph_planet_class = yes }
	}

	immediate = {
		every_owned_planet = {
			# FIX 2026-07: was pc_xenomorph_hive only; wild-colonized Tendril
			# and other Xenomorph world classes never got flag/modifier upkeep.
			limit = { xeno_is_xenomorph_planet_class = yes }

			if = {
				# 3_62 F11: guard removed (colony!=planet scope)
				set_planet_flag = xenomorph_hive_world
			}

			if = {
				limit = { owner = { any_owned_species = { has_trait = trait_xenomorph } } }
				add_modifier = { modifier = xenomorph_hive_world_xeno_mastery days = 370 }
				remove_modifier = xenomorph_hive_world_foreign_research
			}
			else = {
				add_modifier = { modifier = xenomorph_hive_world_foreign_research days = 370 }
				remove_modifier = xenomorph_hive_world_xeno_mastery
			}
		}
	}
}

# Optional event for origins. Call this from an origin startup event if that
# origin should start on a true Xenomorph Hive World.
# Do NOT use this for origin_xenomorph_prime if you want Xenomorph Prime to remain a normal Hive World.
country_event = {
	id = xeno_hive_world.20
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_origin = origin_xenomorph_queen_mother
			# Add more Xenomorph-specific origins here if they should start on pc_xenomorph_hive.
		}
	}

	immediate = {
		capital_scope = {
			set_planet_class = pc_xenomorph_hive
			set_planet_flag = xenomorph_hive_world
			add_modifier = { modifier = xenomorph_hive_world_xeno_mastery days = 370 }
		}
	}
}

###############################################
# === xenomorph_species_events.txt ===
###############################################
###############################################
# Xenomorph Species / Trait / Hostline Events
#
# Contains events tied to Xenomorph species mechanics, traits,
# purge conversion, hostline adaptation, and subspecies unlocks.
###############################################

###############################################
# Xenomorph Necrophage Conversion Events
#
# Implements pop-by-pop conversion when a Xenomorph empire purges 
# non-Xenomorph pops.
#
# Scope contract (set by on_pop_purged):
#   THIS / ROOT  = planet
#   FROM         = the purging country (Xenomorph empire)
#   FROMFROM     = pop group that was just purged
#   local_pop_amount = amount of pops just purged
###############################################

namespace = xeno_necrophage

# Standard Rapid Necrophage Conversion
planet_event = {
	id = xeno_necrophage.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = { is_xenomorph_empire = yes }
		exists = fromfrom
		fromfrom = {
			OR = {
				has_purge_type = purge_xenomorph_rapid_necrophage
				has_purge_type = purge_xenomorph_selective_matriarchal
				has_purge_type = purge_xenomorph_selective_patriarchal
			}
		}
	}

	immediate = {
		# Record the host species class for hostline milestone tracking.
		fromfrom = {
			save_event_target_as = xeno_host_pop_for_class
			owner = { save_event_target_as = xeno_assimilating_country }
		}
		fromfrom = { xeno_record_hostline_assimilation = yes }

		# Resolve which species to spawn (hybrid if unlocked, else base Xenomorph).
		xeno_pick_conversion_species = yes

		# Spawn one pop per purged pop using resolved species.
		while = {
			count = local_pop_amount
			create_pop = {
				species = event_target:xeno_conversion_target_species
				ethos = from
			}
		}

		# Selective Purge completion check.
		# Pass 15: threshold is now empire-wide species count < 1000 (was per-planet
		# count of pop_groups < 3). Once threshold is hit, survivors get the
		# all-male/all-female trait, opposite-sex LV traits are stripped, and the
		# species rights are changed to Breeding Stock, which STOPS the purge
		# (Stellaris purge engine requires the species to have purge rights to keep
		# running; switching to slavery_xenomorph_breeding_stock ends it).
		#
		# Guard: only fire ONCE per species per empire — use a species flag so this
		# block doesn't run again on subsequent pop deaths after threshold is hit.
		if = {
			limit = {
				OR = {
					fromfrom = { has_purge_type = purge_xenomorph_selective_matriarchal }
					fromfrom = { has_purge_type = purge_xenomorph_selective_patriarchal }
				}
				fromfrom = {
					NOT = { has_species_flag = xeno_selective_purge_completed }
				}
				# Empire-wide count of pops of this species < 1000
				from = {
					count_pops = {
						limit = { is_same_species = fromfrom }
						count < 1000
					}
				}
			}

			# Identify the species and lock the block from re-firing.
			fromfrom = {
				save_event_target_as = purged_species
				set_species_flag = xeno_selective_purge_completed
			}

			# Apply changes to the surviving species.
			if = {
				limit = { fromfrom = { has_purge_type = purge_xenomorph_selective_matriarchal } }
				event_target:purged_species = {
					# Strip lv_bio_feminine-tagged traits.
					remove_trait = trait_lv_all_female
					remove_trait = trait_lv_aphrodisiac_milk
					remove_trait = trait_lv_breeder_hips
					remove_trait = trait_lv_brood_parasite
					remove_trait = trait_lv_drooling_vagina
					remove_trait = trait_lv_excess_estrogen
					remove_trait = trait_lv_fizzy_milk
					remove_trait = trait_lv_fuckable_nipples
					remove_trait = trait_lv_high_volume_lactation
					remove_trait = trait_lv_highly_nutritious_milk
					remove_trait = trait_lv_huge_breasts
					remove_trait = trait_lv_intercompatible_womb
					remove_trait = trait_lv_large_breasts
					remove_trait = trait_lv_litters
					remove_trait = trait_lv_multiple_breasts
					remove_trait = trait_lv_nano_forge_womb
					remove_trait = trait_lv_prize_cow
					remove_trait = trait_lv_shapely_ass
					remove_trait = trait_lv_small_breasts
					remove_trait = trait_lv_titanic_evolution
					remove_trait = trait_lv_watery_milk
					# Apply the all-male trait.
					add_trait = trait_lv_all_male
					# Switch species rights to Breeding Stock — this STOPS the purge.
					set_species_rights = {
						type = slavery_xenomorph_breeding_stock
						country = from
					}
				}
				set_variable = { name = xeno_selective_purge_type value = 1 } # Matriarchal
			}
			else = {
				event_target:purged_species = {
					# Strip lv_bio_masculine-tagged traits (per LV trait classification).
					remove_trait = trait_lv_addictive_cum
					remove_trait = trait_lv_all_male
					remove_trait = trait_lv_aphrodisiac_cum
					remove_trait = trait_lv_equine_penis
					remove_trait = trait_lv_excess_testosterone
					remove_trait = trait_lv_high_volume_ejaculations
					remove_trait = trait_lv_huge_penis
					remove_trait = trait_lv_intercompatible_sperm
					remove_trait = trait_lv_large_penis
					remove_trait = trait_lv_multiple_penises
					remove_trait = trait_lv_small_penis
					# Apply the all-female trait.
					add_trait = trait_lv_all_female
					set_species_rights = {
						type = slavery_xenomorph_breeding_stock
						country = from
					}
				}
				set_variable = { name = xeno_selective_purge_type value = 2 } # Patriarchal
			}

			# Fire notification event.
			from = {
				country_event = { id = xeno_necrophage.2 }
			}
		}
	}
}

# Notification: Selective Purge Complete
country_event = {
	id = xeno_necrophage.2
	title = xeno_necrophage.2.name
	desc = xeno_necrophage.2.desc
	picture = GFX_evt_xenomorph_infestation
	show_sound = event_hive_mind_signal
	is_triggered_only = yes

	trigger = {
		is_xenomorph_empire = yes
	}

	option = {
		name = xeno_necrophage.2.a
	}
}

namespace = xenocivic

# FIX M5: Added owner = { any_owned_species = { has_trait = trait_xenomorph } } gate so this event
# does not fire for every empire in the galaxy every year.
# The Endoparasitoid conversion mechanic now fires for ALL Xenomorph empires as a core species trait.

planet_event = {
	id = xenocivic.1
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		owner = {
			any_owned_species = {
				has_trait = trait_xenomorph
			}
		}
		any_owned_pop_group = {
			is_xenomorph = yes
		}
		any_owned_pop_group = {
			is_xenomorph = no
			pop_group_size >= 100
		}
	}
	immediate = {
		# Pick a non-Xeno pop group to convert.
		weighted_random_owned_pop_group = {
			limit = {
				is_xenomorph = no
				pop_group_size >= 100
			}
			save_event_target_as = xeno_host_pop_for_class
			kill_single_pop = yes
		}

		# Record host class for milestone tracking.
		event_target:xeno_host_pop_for_class = {
			xeno_record_hostline_assimilation = yes
		}

		# Resolve which species to spawn (hybrid if unlocked, else base Xenomorph).
		xeno_pick_conversion_species = yes

		# Spawn 100 pops of resolved species.
		create_pop_group = {
			species = event_target:xeno_conversion_target_species
			size = 100
		}
	}
}

namespace = xeno_hostline

# Monthly hidden checker.
country_event = {
	id = xeno_hostline.1
	hide_window = yes
	is_triggered_only = yes

	trigger = { xeno_is_xenomorph_hive_country = yes }

	immediate = {
		# Reptilian Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_reptilian_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_reptilian_stage_1_prompted }
			}
			set_country_flag = xeno_reptilian_stage_1_prompted
			country_event = { id = xeno_hostline.100 }
		}

		# Reptilian ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_reptilian_hosts_consumed value >= 20 }
				has_country_flag = xeno_reptilian_stage_1_ignored
				NOT = { has_country_flag = xeno_reptilian_stage_1_second_prompted }
			}
			set_country_flag = xeno_reptilian_stage_1_second_prompted
			country_event = { id = xeno_hostline.101 }
		}

		# Reptilian Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_reptilian_hosts_consumed value >= 20 }
				has_country_flag = xeno_reptilian_hostline_unlocked
				NOT = { has_country_flag = xeno_reptilian_stage_2_prompted }
			}
			set_country_flag = xeno_reptilian_stage_2_prompted
			country_event = { id = xeno_hostline.110 }
		}

		# Reptilian study progress handlers.
		if = {
			limit = { has_country_flag = xeno_reptilian_hostline_study_active }
			change_variable = { which = xeno_reptilian_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_reptilian_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.120 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_reptilian_hostline_refinement_active }
			change_variable = { which = xeno_reptilian_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_reptilian_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.121 }
			}
		}

		# Avian Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_avian_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_avian_stage_1_prompted }
			}
			set_country_flag = xeno_avian_stage_1_prompted
			country_event = { id = xeno_hostline.200 }
		}

		# Avian ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_avian_hosts_consumed value >= 20 }
				has_country_flag = xeno_avian_stage_1_ignored
				NOT = { has_country_flag = xeno_avian_stage_1_second_prompted }
			}
			set_country_flag = xeno_avian_stage_1_second_prompted
			country_event = { id = xeno_hostline.201 }
		}

		# Avian Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_avian_hosts_consumed value >= 20 }
				has_country_flag = xeno_avian_hostline_unlocked
				NOT = { has_country_flag = xeno_avian_stage_2_prompted }
			}
			set_country_flag = xeno_avian_stage_2_prompted
			country_event = { id = xeno_hostline.210 }
		}

		# Avian study progress handlers.
		if = {
			limit = { has_country_flag = xeno_avian_hostline_study_active }
			change_variable = { which = xeno_avian_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_avian_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.220 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_avian_hostline_refinement_active }
			change_variable = { which = xeno_avian_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_avian_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.221 }
			}
		}

		# Mammalian Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_mammalian_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_mammalian_stage_1_prompted }
			}
			set_country_flag = xeno_mammalian_stage_1_prompted
			country_event = { id = xeno_hostline.300 }
		}

		# Mammalian ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_mammalian_hosts_consumed value >= 20 }
				has_country_flag = xeno_mammalian_stage_1_ignored
				NOT = { has_country_flag = xeno_mammalian_stage_1_second_prompted }
			}
			set_country_flag = xeno_mammalian_stage_1_second_prompted
			country_event = { id = xeno_hostline.301 }
		}

		# Mammalian Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_mammalian_hosts_consumed value >= 20 }
				has_country_flag = xeno_mammalian_hostline_unlocked
				NOT = { has_country_flag = xeno_mammalian_stage_2_prompted }
			}
			set_country_flag = xeno_mammalian_stage_2_prompted
			country_event = { id = xeno_hostline.310 }
		}

		# Mammalian study progress handlers.
		if = {
			limit = { has_country_flag = xeno_mammalian_hostline_study_active }
			change_variable = { which = xeno_mammalian_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_mammalian_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.320 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_mammalian_hostline_refinement_active }
			change_variable = { which = xeno_mammalian_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_mammalian_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.321 }
			}
		}

		# Arthropoid Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_arthropoid_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_arthropoid_stage_1_prompted }
			}
			set_country_flag = xeno_arthropoid_stage_1_prompted
			country_event = { id = xeno_hostline.400 }
		}

		# Arthropoid ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_arthropoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_arthropoid_stage_1_ignored
				NOT = { has_country_flag = xeno_arthropoid_stage_1_second_prompted }
			}
			set_country_flag = xeno_arthropoid_stage_1_second_prompted
			country_event = { id = xeno_hostline.401 }
		}

		# Arthropoid Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_arthropoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_arthropoid_hostline_unlocked
				NOT = { has_country_flag = xeno_arthropoid_stage_2_prompted }
			}
			set_country_flag = xeno_arthropoid_stage_2_prompted
			country_event = { id = xeno_hostline.410 }
		}

		# Arthropoid study progress handlers.
		if = {
			limit = { has_country_flag = xeno_arthropoid_hostline_study_active }
			change_variable = { which = xeno_arthropoid_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_arthropoid_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.420 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_arthropoid_hostline_refinement_active }
			change_variable = { which = xeno_arthropoid_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_arthropoid_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.421 }
			}
		}

		# Molluscoid Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_molluscoid_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_molluscoid_stage_1_prompted }
			}
			set_country_flag = xeno_molluscoid_stage_1_prompted
			country_event = { id = xeno_hostline.500 }
		}

		# Molluscoid ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_molluscoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_molluscoid_stage_1_ignored
				NOT = { has_country_flag = xeno_molluscoid_stage_1_second_prompted }
			}
			set_country_flag = xeno_molluscoid_stage_1_second_prompted
			country_event = { id = xeno_hostline.501 }
		}

		# Molluscoid Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_molluscoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_molluscoid_hostline_unlocked
				NOT = { has_country_flag = xeno_molluscoid_stage_2_prompted }
			}
			set_country_flag = xeno_molluscoid_stage_2_prompted
			country_event = { id = xeno_hostline.510 }
		}

		# Molluscoid study progress handlers.
		if = {
			limit = { has_country_flag = xeno_molluscoid_hostline_study_active }
			change_variable = { which = xeno_molluscoid_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_molluscoid_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.520 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_molluscoid_hostline_refinement_active }
			change_variable = { which = xeno_molluscoid_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_molluscoid_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.521 }
			}
		}

		# Lithoid Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_lithoid_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_lithoid_stage_1_prompted }
			}
			set_country_flag = xeno_lithoid_stage_1_prompted
			country_event = { id = xeno_hostline.600 }
		}

		# Lithoid ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_lithoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_lithoid_stage_1_ignored
				NOT = { has_country_flag = xeno_lithoid_stage_1_second_prompted }
			}
			set_country_flag = xeno_lithoid_stage_1_second_prompted
			country_event = { id = xeno_hostline.601 }
		}

		# Lithoid Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_lithoid_hosts_consumed value >= 20 }
				has_country_flag = xeno_lithoid_hostline_unlocked
				NOT = { has_country_flag = xeno_lithoid_stage_2_prompted }
			}
			set_country_flag = xeno_lithoid_stage_2_prompted
			country_event = { id = xeno_hostline.610 }
		}

		# Lithoid study progress handlers.
		if = {
			limit = { has_country_flag = xeno_lithoid_hostline_study_active }
			change_variable = { which = xeno_lithoid_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_lithoid_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.620 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_lithoid_hostline_refinement_active }
			change_variable = { which = xeno_lithoid_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_lithoid_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.621 }
			}
		}

		# Abyssal Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_aquatic_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_aquatic_stage_1_prompted }
			}
			set_country_flag = xeno_aquatic_stage_1_prompted
			country_event = { id = xeno_hostline.700 }
		}

		# Abyssal ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_aquatic_hosts_consumed value >= 20 }
				has_country_flag = xeno_aquatic_stage_1_ignored
				NOT = { has_country_flag = xeno_aquatic_stage_1_second_prompted }
			}
			set_country_flag = xeno_aquatic_stage_1_second_prompted
			country_event = { id = xeno_hostline.701 }
		}

		# Abyssal Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_aquatic_hosts_consumed value >= 20 }
				has_country_flag = xeno_aquatic_hostline_unlocked
				NOT = { has_country_flag = xeno_aquatic_stage_2_prompted }
			}
			set_country_flag = xeno_aquatic_stage_2_prompted
			country_event = { id = xeno_hostline.710 }
		}

		# Abyssal study progress handlers.
		if = {
			limit = { has_country_flag = xeno_aquatic_hostline_study_active }
			change_variable = { which = xeno_aquatic_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_aquatic_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.720 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_aquatic_hostline_refinement_active }
			change_variable = { which = xeno_aquatic_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_aquatic_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.721 }
			}
		}

		# Cadaverous Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_necroid_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_necroid_stage_1_prompted }
			}
			set_country_flag = xeno_necroid_stage_1_prompted
			country_event = { id = xeno_hostline.800 }
		}

		# Cadaverous ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_necroid_hosts_consumed value >= 20 }
				has_country_flag = xeno_necroid_stage_1_ignored
				NOT = { has_country_flag = xeno_necroid_stage_1_second_prompted }
			}
			set_country_flag = xeno_necroid_stage_1_second_prompted
			country_event = { id = xeno_hostline.801 }
		}

		# Cadaverous Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_necroid_hosts_consumed value >= 20 }
				has_country_flag = xeno_necroid_hostline_unlocked
				NOT = { has_country_flag = xeno_necroid_stage_2_prompted }
			}
			set_country_flag = xeno_necroid_stage_2_prompted
			country_event = { id = xeno_hostline.810 }
		}

		# Cadaverous study progress handlers.
		if = {
			limit = { has_country_flag = xeno_necroid_hostline_study_active }
			change_variable = { which = xeno_necroid_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_necroid_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.820 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_necroid_hostline_refinement_active }
			change_variable = { which = xeno_necroid_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_necroid_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.821 }
			}
		}

		# Psionic Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_psionic_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_psionic_stage_1_prompted }
			}
			set_country_flag = xeno_psionic_stage_1_prompted
			country_event = { id = xeno_hostline.900 }
		}

		# Psionic ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_psionic_hosts_consumed value >= 20 }
				has_country_flag = xeno_psionic_stage_1_ignored
				NOT = { has_country_flag = xeno_psionic_stage_1_second_prompted }
			}
			set_country_flag = xeno_psionic_stage_1_second_prompted
			country_event = { id = xeno_hostline.901 }
		}

		# Psionic Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_psionic_hosts_consumed value >= 20 }
				has_country_flag = xeno_psionic_hostline_unlocked
				NOT = { has_country_flag = xeno_psionic_stage_2_prompted }
			}
			set_country_flag = xeno_psionic_stage_2_prompted
			country_event = { id = xeno_hostline.910 }
		}

		# Psionic special unlock: research must be completed before the subspecies unlocks.
		if = {
			limit = {
				has_technology = tech_xenomorph_psionic_hostline_adaptation
				has_country_flag = xeno_psionic_hostline_tech_available
				NOT = { has_country_flag = xeno_psionic_hostline_unlocked }
			}
			set_country_flag = xeno_psionic_hostline_unlocked
			country_event = { id = xeno_hostline.930 }
		}

		# Psionic study progress handlers.
		if = {
			limit = { has_country_flag = xeno_psionic_hostline_study_active }
			change_variable = { which = xeno_psionic_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_psionic_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.920 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_psionic_hostline_refinement_active }
			change_variable = { which = xeno_psionic_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_psionic_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.921 }
			}
		}

		# Cybernetic Stage 1: first study chance at 100 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_cybernetic_hosts_consumed value >= 5 }
				NOT = { has_country_flag = xeno_cybernetic_stage_1_prompted }
			}
			set_country_flag = xeno_cybernetic_stage_1_prompted
			country_event = { id = xeno_hostline.1000 }
		}

		# Cybernetic ignored Stage 1: second chance at 1000 consumed.
		if = {
			limit = {
				check_variable = { which = xeno_cybernetic_hosts_consumed value >= 20 }
				has_country_flag = xeno_cybernetic_stage_1_ignored
				NOT = { has_country_flag = xeno_cybernetic_stage_1_second_prompted }
			}
			set_country_flag = xeno_cybernetic_stage_1_second_prompted
			country_event = { id = xeno_hostline.1001 }
		}

		# Cybernetic Stage 2: enhancement chance at 1000, if Stage 1 was actually unlocked.
		if = {
			limit = {
				check_variable = { which = xeno_cybernetic_hosts_consumed value >= 20 }
				has_country_flag = xeno_cybernetic_hostline_unlocked
				NOT = { has_country_flag = xeno_cybernetic_stage_2_prompted }
			}
			set_country_flag = xeno_cybernetic_stage_2_prompted
			country_event = { id = xeno_hostline.1010 }
		}

		# Cybernetic special unlock: research must be completed before the subspecies unlocks.
		if = {
			limit = {
				has_technology = tech_xenomorph_cybernetic_hostline_adaptation
				has_country_flag = xeno_cybernetic_hostline_tech_available
				NOT = { has_country_flag = xeno_cybernetic_hostline_unlocked }
			}
			set_country_flag = xeno_cybernetic_hostline_unlocked
			country_event = { id = xeno_hostline.1030 }
		}

		# Cybernetic study progress handlers.
		if = {
			limit = { has_country_flag = xeno_cybernetic_hostline_study_active }
			change_variable = { which = xeno_cybernetic_hostline_study_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_cybernetic_hostline_study_months value >= 24 } }
				country_event = { id = xeno_hostline.1020 }
			}
		}

		if = {
			limit = { has_country_flag = xeno_cybernetic_hostline_refinement_active }
			change_variable = { which = xeno_cybernetic_hostline_refinement_months value = 1 }
			if = {
				limit = { check_variable = { which = xeno_cybernetic_hostline_refinement_months value >= 36 } }
				country_event = { id = xeno_hostline.1021 }
			}
		}

	}
}

country_event = {
	id = xeno_hostline.100
	title = xeno_hostline.100.name
	desc = xeno_hostline.100.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.100.a
		set_country_flag = xeno_reptilian_hostline_study_active
		set_variable = { which = xeno_reptilian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_reptilian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.100.b
		set_country_flag = xeno_reptilian_stage_1_ignored
		add_modifier = { modifier = xeno_reptilian_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.101
	title = xeno_hostline.101.name
	desc = xeno_hostline.101.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.101.a
		set_country_flag = xeno_reptilian_hostline_study_active
		set_variable = { which = xeno_reptilian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_reptilian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.101.b
		add_modifier = { modifier = xeno_reptilian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.110
	title = xeno_hostline.110.name
	desc = xeno_hostline.110.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.110.a
		set_country_flag = xeno_reptilian_hostline_refinement_active
		set_variable = { which = xeno_reptilian_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_reptilian_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.110.b
		set_country_flag = xeno_reptilian_stage_2_ignored
		add_modifier = { modifier = xeno_reptilian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.120
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_reptilian_hostline_study_active
		remove_modifier = xeno_reptilian_hostline_study_cost
		set_country_flag = xeno_reptilian_hostline_tech_available
		add_research_option = tech_xenomorph_reptilian_hostline_adaptation
		set_country_flag = xeno_reptilian_hostline_unlocked
		country_event = { id = xeno_hostline.130 }
	}
}

country_event = {
	id = xeno_hostline.121
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the reptilian hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_reptilian_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_reptilian = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_reptilian_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_reptilian_hybrid_template
					}
				}
				set_country_flag = xeno_reptilian_hybrid_species_created
				# First-ever hybrid: fire the emergence popup (once per empire).
				if = {
					limit = { NOT = { has_country_flag = xeno_first_hybrid_emerged } }
					set_country_flag = xeno_first_hybrid_emerged
					country_event = { id = xeno_hybrid.1 }
				}
			}
		}

		remove_country_flag = xeno_reptilian_hostline_refinement_active
		remove_modifier = xeno_reptilian_hostline_refinement_cost
		set_country_flag = xeno_reptilian_hostline_refined
		country_event = { id = xeno_hostline.131 }
	}
}

country_event = {
	id = xeno_hostline.130
	title = xeno_hostline.130.name
	desc = xeno_hostline.130.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.131
	title = xeno_hostline.131.name
	desc = xeno_hostline.131.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.200
	title = xeno_hostline.200.name
	desc = xeno_hostline.200.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.200.a
		set_country_flag = xeno_avian_hostline_study_active
		set_variable = { which = xeno_avian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_avian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.200.b
		set_country_flag = xeno_avian_stage_1_ignored
		add_modifier = { modifier = xeno_avian_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.201
	title = xeno_hostline.201.name
	desc = xeno_hostline.201.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.201.a
		set_country_flag = xeno_avian_hostline_study_active
		set_variable = { which = xeno_avian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_avian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.201.b
		add_modifier = { modifier = xeno_avian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.210
	title = xeno_hostline.210.name
	desc = xeno_hostline.210.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.210.a
		set_country_flag = xeno_avian_hostline_refinement_active
		set_variable = { which = xeno_avian_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_avian_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.210.b
		set_country_flag = xeno_avian_stage_2_ignored
		add_modifier = { modifier = xeno_avian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.220
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_avian_hostline_study_active
		remove_modifier = xeno_avian_hostline_study_cost
		set_country_flag = xeno_avian_hostline_tech_available
		add_research_option = tech_xenomorph_avian_hostline_adaptation
		set_country_flag = xeno_avian_hostline_unlocked
		country_event = { id = xeno_hostline.230 }
	}
}

country_event = {
	id = xeno_hostline.221
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the avian hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_avian_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_avian = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_arboreal_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_avian_hybrid_template
					}
				}
				set_country_flag = xeno_avian_hybrid_species_created
			}
		}

		remove_country_flag = xeno_avian_hostline_refinement_active
		remove_modifier = xeno_avian_hostline_refinement_cost
		set_country_flag = xeno_avian_hostline_refined
		country_event = { id = xeno_hostline.231 }
	}
}

country_event = {
	id = xeno_hostline.230
	title = xeno_hostline.230.name
	desc = xeno_hostline.230.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.231
	title = xeno_hostline.231.name
	desc = xeno_hostline.231.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.300
	title = xeno_hostline.300.name
	desc = xeno_hostline.300.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.300.a
		set_country_flag = xeno_mammalian_hostline_study_active
		set_variable = { which = xeno_mammalian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_mammalian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.300.b
		set_country_flag = xeno_mammalian_stage_1_ignored
		add_modifier = { modifier = xeno_mammalian_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.301
	title = xeno_hostline.301.name
	desc = xeno_hostline.301.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.301.a
		set_country_flag = xeno_mammalian_hostline_study_active
		set_variable = { which = xeno_mammalian_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_mammalian_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.301.b
		add_modifier = { modifier = xeno_mammalian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.310
	title = xeno_hostline.310.name
	desc = xeno_hostline.310.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.310.a
		set_country_flag = xeno_mammalian_hostline_refinement_active
		set_variable = { which = xeno_mammalian_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_mammalian_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.310.b
		set_country_flag = xeno_mammalian_stage_2_ignored
		add_modifier = { modifier = xeno_mammalian_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.320
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_mammalian_hostline_study_active
		remove_modifier = xeno_mammalian_hostline_study_cost
		set_country_flag = xeno_mammalian_hostline_tech_available
		add_research_option = tech_xenomorph_mammalian_hostline_adaptation
		set_country_flag = xeno_mammalian_hostline_unlocked
		country_event = { id = xeno_hostline.330 }
	}
}

country_event = {
	id = xeno_hostline.321
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the mammalian hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_mammalian_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_mammalian = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_mammalian_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_mammalian_hybrid_template
					}
				}
				set_country_flag = xeno_mammalian_hybrid_species_created
			}
		}

		remove_country_flag = xeno_mammalian_hostline_refinement_active
		remove_modifier = xeno_mammalian_hostline_refinement_cost
		set_country_flag = xeno_mammalian_hostline_refined
		country_event = { id = xeno_hostline.331 }
	}
}

country_event = {
	id = xeno_hostline.330
	title = xeno_hostline.330.name
	desc = xeno_hostline.330.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.331
	title = xeno_hostline.331.name
	desc = xeno_hostline.331.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.400
	title = xeno_hostline.400.name
	desc = xeno_hostline.400.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.400.a
		set_country_flag = xeno_arthropoid_hostline_study_active
		set_variable = { which = xeno_arthropoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_arthropoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.400.b
		set_country_flag = xeno_arthropoid_stage_1_ignored
		add_modifier = { modifier = xeno_arthropoid_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.401
	title = xeno_hostline.401.name
	desc = xeno_hostline.401.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.401.a
		set_country_flag = xeno_arthropoid_hostline_study_active
		set_variable = { which = xeno_arthropoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_arthropoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.401.b
		add_modifier = { modifier = xeno_arthropoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.410
	title = xeno_hostline.410.name
	desc = xeno_hostline.410.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.410.a
		set_country_flag = xeno_arthropoid_hostline_refinement_active
		set_variable = { which = xeno_arthropoid_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_arthropoid_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.410.b
		set_country_flag = xeno_arthropoid_stage_2_ignored
		add_modifier = { modifier = xeno_arthropoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.420
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_arthropoid_hostline_study_active
		remove_modifier = xeno_arthropoid_hostline_study_cost
		set_country_flag = xeno_arthropoid_hostline_tech_available
		add_research_option = tech_xenomorph_arthropoid_hostline_adaptation
		set_country_flag = xeno_arthropoid_hostline_unlocked
		country_event = { id = xeno_hostline.430 }
	}
}

country_event = {
	id = xeno_hostline.421
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the arthropoid hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_arthropoid_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_arthropoid = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_acid_gland
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_arthropoid_hybrid_template
					}
				}
				set_country_flag = xeno_arthropoid_hybrid_species_created
			}
		}

		remove_country_flag = xeno_arthropoid_hostline_refinement_active
		remove_modifier = xeno_arthropoid_hostline_refinement_cost
		set_country_flag = xeno_arthropoid_hostline_refined
		country_event = { id = xeno_hostline.431 }
	}
}

country_event = {
	id = xeno_hostline.430
	title = xeno_hostline.430.name
	desc = xeno_hostline.430.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.431
	title = xeno_hostline.431.name
	desc = xeno_hostline.431.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.500
	title = xeno_hostline.500.name
	desc = xeno_hostline.500.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.500.a
		set_country_flag = xeno_molluscoid_hostline_study_active
		set_variable = { which = xeno_molluscoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_molluscoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.500.b
		set_country_flag = xeno_molluscoid_stage_1_ignored
		add_modifier = { modifier = xeno_molluscoid_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.501
	title = xeno_hostline.501.name
	desc = xeno_hostline.501.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.501.a
		set_country_flag = xeno_molluscoid_hostline_study_active
		set_variable = { which = xeno_molluscoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_molluscoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.501.b
		add_modifier = { modifier = xeno_molluscoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.510
	title = xeno_hostline.510.name
	desc = xeno_hostline.510.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.510.a
		set_country_flag = xeno_molluscoid_hostline_refinement_active
		set_variable = { which = xeno_molluscoid_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_molluscoid_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.510.b
		set_country_flag = xeno_molluscoid_stage_2_ignored
		add_modifier = { modifier = xeno_molluscoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.520
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_molluscoid_hostline_study_active
		remove_modifier = xeno_molluscoid_hostline_study_cost
		set_country_flag = xeno_molluscoid_hostline_tech_available
		add_research_option = tech_xenomorph_molluscoid_hostline_adaptation
		set_country_flag = xeno_molluscoid_hostline_unlocked
		country_event = { id = xeno_hostline.530 }
	}
}

country_event = {
	id = xeno_hostline.521
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the molluscoid hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_molluscoid_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_molluscoid = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_aquatic_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_molluscoid_hybrid_template
					}
				}
				set_country_flag = xeno_molluscoid_hybrid_species_created
			}
		}

		remove_country_flag = xeno_molluscoid_hostline_refinement_active
		remove_modifier = xeno_molluscoid_hostline_refinement_cost
		set_country_flag = xeno_molluscoid_hostline_refined
		country_event = { id = xeno_hostline.531 }
	}
}

country_event = {
	id = xeno_hostline.530
	title = xeno_hostline.530.name
	desc = xeno_hostline.530.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.531
	title = xeno_hostline.531.name
	desc = xeno_hostline.531.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.600
	title = xeno_hostline.600.name
	desc = xeno_hostline.600.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.600.a
		set_country_flag = xeno_lithoid_hostline_study_active
		set_variable = { which = xeno_lithoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_lithoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.600.b
		set_country_flag = xeno_lithoid_stage_1_ignored
		add_modifier = { modifier = xeno_lithoid_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.601
	title = xeno_hostline.601.name
	desc = xeno_hostline.601.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.601.a
		set_country_flag = xeno_lithoid_hostline_study_active
		set_variable = { which = xeno_lithoid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_lithoid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.601.b
		add_modifier = { modifier = xeno_lithoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.610
	title = xeno_hostline.610.name
	desc = xeno_hostline.610.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.610.a
		set_country_flag = xeno_lithoid_hostline_refinement_active
		set_variable = { which = xeno_lithoid_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_lithoid_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.610.b
		set_country_flag = xeno_lithoid_stage_2_ignored
		add_modifier = { modifier = xeno_lithoid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.620
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_lithoid_hostline_study_active
		remove_modifier = xeno_lithoid_hostline_study_cost
		set_country_flag = xeno_lithoid_hostline_tech_available
		add_research_option = tech_xenomorph_lithoid_hostline_adaptation
		set_country_flag = xeno_lithoid_hostline_unlocked
		country_event = { id = xeno_hostline.630 }
	}
}

country_event = {
	id = xeno_hostline.621
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the lithoid hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_lithoid_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_lithoid = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_lithoid_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_lithoid_hybrid_template
					}
				}
				set_country_flag = xeno_lithoid_hybrid_species_created
			}
		}

		remove_country_flag = xeno_lithoid_hostline_refinement_active
		remove_modifier = xeno_lithoid_hostline_refinement_cost
		set_country_flag = xeno_lithoid_hostline_refined
		country_event = { id = xeno_hostline.631 }
	}
}

country_event = {
	id = xeno_hostline.630
	title = xeno_hostline.630.name
	desc = xeno_hostline.630.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.631
	title = xeno_hostline.631.name
	desc = xeno_hostline.631.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.700
	title = xeno_hostline.700.name
	desc = xeno_hostline.700.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.700.a
		set_country_flag = xeno_aquatic_hostline_study_active
		set_variable = { which = xeno_aquatic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_aquatic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.700.b
		set_country_flag = xeno_aquatic_stage_1_ignored
		add_modifier = { modifier = xeno_aquatic_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.701
	title = xeno_hostline.701.name
	desc = xeno_hostline.701.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.701.a
		set_country_flag = xeno_aquatic_hostline_study_active
		set_variable = { which = xeno_aquatic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_aquatic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.701.b
		add_modifier = { modifier = xeno_aquatic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.710
	title = xeno_hostline.710.name
	desc = xeno_hostline.710.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.710.a
		set_country_flag = xeno_aquatic_hostline_refinement_active
		set_variable = { which = xeno_aquatic_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_aquatic_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.710.b
		set_country_flag = xeno_aquatic_stage_2_ignored
		add_modifier = { modifier = xeno_aquatic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.720
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_aquatic_hostline_study_active
		remove_modifier = xeno_aquatic_hostline_study_cost
		set_country_flag = xeno_aquatic_hostline_tech_available
		add_research_option = tech_xenomorph_aquatic_hostline_adaptation
		set_country_flag = xeno_aquatic_hostline_unlocked
		country_event = { id = xeno_hostline.730 }
	}
}

country_event = {
	id = xeno_hostline.721
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the aquatic hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_aquatic_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_aquatic = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_aquatic_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_aquatic_hybrid_template
					}
				}
				set_country_flag = xeno_aquatic_hybrid_species_created
			}
		}

		remove_country_flag = xeno_aquatic_hostline_refinement_active
		remove_modifier = xeno_aquatic_hostline_refinement_cost
		set_country_flag = xeno_aquatic_hostline_refined
		country_event = { id = xeno_hostline.731 }
	}
}

country_event = {
	id = xeno_hostline.730
	title = xeno_hostline.730.name
	desc = xeno_hostline.730.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.731
	title = xeno_hostline.731.name
	desc = xeno_hostline.731.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.800
	title = xeno_hostline.800.name
	desc = xeno_hostline.800.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.800.a
		set_country_flag = xeno_necroid_hostline_study_active
		set_variable = { which = xeno_necroid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_necroid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.800.b
		set_country_flag = xeno_necroid_stage_1_ignored
		add_modifier = { modifier = xeno_necroid_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.801
	title = xeno_hostline.801.name
	desc = xeno_hostline.801.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.801.a
		set_country_flag = xeno_necroid_hostline_study_active
		set_variable = { which = xeno_necroid_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_necroid_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.801.b
		add_modifier = { modifier = xeno_necroid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.810
	title = xeno_hostline.810.name
	desc = xeno_hostline.810.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.810.a
		set_country_flag = xeno_necroid_hostline_refinement_active
		set_variable = { which = xeno_necroid_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_necroid_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.810.b
		set_country_flag = xeno_necroid_stage_2_ignored
		add_modifier = { modifier = xeno_necroid_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.820
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_necroid_hostline_study_active
		remove_modifier = xeno_necroid_hostline_study_cost
		set_country_flag = xeno_necroid_hostline_tech_available
		add_research_option = tech_xenomorph_necroid_hostline_adaptation
		set_country_flag = xeno_necroid_hostline_unlocked
		country_event = { id = xeno_hostline.830 }
	}
}

country_event = {
	id = xeno_hostline.821
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the necroid hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_necroid_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_necroid = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_spore_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_necroid_hybrid_template
					}
				}
				set_country_flag = xeno_necroid_hybrid_species_created
			}
		}

		remove_country_flag = xeno_necroid_hostline_refinement_active
		remove_modifier = xeno_necroid_hostline_refinement_cost
		set_country_flag = xeno_necroid_hostline_refined
		country_event = { id = xeno_hostline.831 }
	}
}

country_event = {
	id = xeno_hostline.830
	title = xeno_hostline.830.name
	desc = xeno_hostline.830.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.831
	title = xeno_hostline.831.name
	desc = xeno_hostline.831.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.900
	title = xeno_hostline.900.name
	desc = xeno_hostline.900.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.900.a
		set_country_flag = xeno_psionic_hostline_study_active
		set_variable = { which = xeno_psionic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_psionic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.900.b
		set_country_flag = xeno_psionic_stage_1_ignored
		add_modifier = { modifier = xeno_psionic_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.901
	title = xeno_hostline.901.name
	desc = xeno_hostline.901.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.901.a
		set_country_flag = xeno_psionic_hostline_study_active
		set_variable = { which = xeno_psionic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_psionic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.901.b
		add_modifier = { modifier = xeno_psionic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.910
	title = xeno_hostline.910.name
	desc = xeno_hostline.910.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.910.a
		set_country_flag = xeno_psionic_hostline_refinement_active
		set_variable = { which = xeno_psionic_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_psionic_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.910.b
		set_country_flag = xeno_psionic_stage_2_ignored
		add_modifier = { modifier = xeno_psionic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.920
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_psionic_hostline_study_active
		remove_modifier = xeno_psionic_hostline_study_cost
		set_country_flag = xeno_psionic_hostline_tech_available
		add_research_option = tech_xenomorph_psionic_hostline_adaptation
		country_event = { id = xeno_hostline.932 }
	}
}

country_event = {
	id = xeno_hostline.921
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the psionic hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_psionic_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_psionic = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_adrenal
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_psionic_hybrid_template
					}
				}
				set_country_flag = xeno_psionic_hybrid_species_created
			}
		}

		remove_country_flag = xeno_psionic_hostline_refinement_active
		remove_modifier = xeno_psionic_hostline_refinement_cost
		set_country_flag = xeno_psionic_hostline_refined
		country_event = { id = xeno_hostline.931 }
	}
}

country_event = {
	id = xeno_hostline.930
	title = xeno_hostline.930.name
	desc = xeno_hostline.930.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.931
	title = xeno_hostline.931.name
	desc = xeno_hostline.931.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.932
	title = xeno_hostline.932.name
	desc = xeno_hostline.932.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.1000
	title = xeno_hostline.1000.name
	desc = xeno_hostline.1000.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.1000.a
		set_country_flag = xeno_cybernetic_hostline_study_active
		set_variable = { which = xeno_cybernetic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_cybernetic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.1000.b
		set_country_flag = xeno_cybernetic_stage_1_ignored
		add_modifier = { modifier = xeno_cybernetic_biomass_ignored days = 3600 }
	}
}

country_event = {
	id = xeno_hostline.1001
	title = xeno_hostline.1001.name
	desc = xeno_hostline.1001.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.1001.a
		set_country_flag = xeno_cybernetic_hostline_study_active
		set_variable = { which = xeno_cybernetic_hostline_study_months value = 0 }
		add_modifier = { modifier = xeno_cybernetic_hostline_study_cost days = -1 }
	}

	option = {
		name = xeno_hostline.1001.b
		add_modifier = { modifier = xeno_cybernetic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.1010
	title = xeno_hostline.1010.name
	desc = xeno_hostline.1010.desc
	picture = GFX_evt_alien_nature
	show_sound = event_alien_signal
	is_triggered_only = yes

	option = {
		name = xeno_hostline.1010.a
		set_country_flag = xeno_cybernetic_hostline_refinement_active
		set_variable = { which = xeno_cybernetic_hostline_refinement_months value = 0 }
		add_modifier = { modifier = xeno_cybernetic_hostline_refinement_cost days = -1 }
	}

	option = {
		name = xeno_hostline.1010.b
		set_country_flag = xeno_cybernetic_stage_2_ignored
		add_modifier = { modifier = xeno_cybernetic_biomass_ignored days = 7200 }
	}
}

country_event = {
	id = xeno_hostline.1020
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_country_flag = xeno_cybernetic_hostline_study_active
		remove_modifier = xeno_cybernetic_hostline_study_cost
		set_country_flag = xeno_cybernetic_hostline_tech_available
		add_research_option = tech_xenomorph_cybernetic_hostline_adaptation
		country_event = { id = xeno_hostline.1032 }
	}
}

country_event = {
	id = xeno_hostline.1021
	hide_window = yes
	is_triggered_only = yes
	immediate = {

		# Create the cybernetic hybrid template species (one-time, idempotent).
		if = {
			limit = {
				NOT = { has_country_flag = xeno_cybernetic_hybrid_species_created }
			}
			# Find any pop of the host class to template from.
			random_owned_pop_group = {
				limit = {
					is_xenomorph = no
					xeno_host_is_cybernetic = yes
				}
				species = { save_event_target_as = xeno_hybrid_host_template }
			}

			if = {
				limit = { exists = event_target:xeno_hybrid_host_template }
				save_event_target_as = xeno_hybrid_class_trait_country
				create_species = {
					name = event_target:xeno_hybrid_host_template
					namelist = event_target:xeno_hybrid_host_template
					portrait = event_target:xeno_hybrid_host_template
					plural = event_target:xeno_hybrid_host_template
					class = event_target:xeno_hybrid_host_template
					homeworld = capital_scope
					traits = {
						trait = trait_xenomorph
						trait = trait_xeno_cybernetic_host
						ingame_modding = yes
					}
					effect = {
						set_species_flag = xeno_hybrid_species
						set_species_flag = xeno_cybernetic_hybrid_template
					}
				}
				set_country_flag = xeno_cybernetic_hybrid_species_created
			}
		}

		remove_country_flag = xeno_cybernetic_hostline_refinement_active
		remove_modifier = xeno_cybernetic_hostline_refinement_cost
		set_country_flag = xeno_cybernetic_hostline_refined
		country_event = { id = xeno_hostline.1031 }
	}
}

country_event = {
	id = xeno_hostline.1030
	title = xeno_hostline.1030.name
	desc = xeno_hostline.1030.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.1031
	title = xeno_hostline.1031.name
	desc = xeno_hostline.1031.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

country_event = {
	id = xeno_hostline.1032
	title = xeno_hostline.1032.name
	desc = xeno_hostline.1032.desc
	picture = GFX_evt_alien_nature
	show_sound = event_administrative_work
	is_triggered_only = yes
	option = { name = xeno_hostline.ok }
}

###############################################
# Xenomorph Situation Completion Events
#
# FIX: situations xenomorph_voidbrood_adaptation and
# xenomorph_dragon_genome_study reference xeno_voidbrood.100 and
# xeno_draconic.100 in their on_complete blocks, but those events were
# never written. Without them the situations silently fail to complete.
#
# Each event uses the matching subspecies tech-unlock pattern from
# xenomorph_general_events.txt: set the unlock flag, surface the tech to
# the player, and notify them.
###############################################

namespace = xeno_voidbrood

country_event = {
	id = xeno_voidbrood.100
	title = xeno_voidbrood.100.name
	desc = xeno_voidbrood.100.desc
	picture = GFX_evt_space_amoeba
	is_triggered_only = yes

	trigger = {
		any_owned_species = { has_trait = trait_xenomorph }
	}

	immediate = {
		set_country_flag = xeno_voidbrood_hostline_tech_available
		add_research_option = tech_xenomorph_voidbrood_adaptation
	}

	option = {
		name = xeno_voidbrood.100.a
		add_resource = { society_research = 1500 }
	}
}

namespace = xeno_draconic

country_event = {
	id = xeno_draconic.100
	title = xeno_draconic.100.name
	desc = xeno_draconic.100.desc
	picture = GFX_evt_space_dragon
	is_triggered_only = yes

	trigger = {
		any_owned_species = { has_trait = trait_xenomorph }
	}

	immediate = {
		set_country_flag = xeno_draconic_hostline_tech_available
		add_research_option = tech_xenomorph_draconic_adaptation
	}

	option = {
		name = xeno_draconic.100.a
		add_resource = { society_research = 2500 engineering_research = 1000 }
	}
}

# =====================================================================
# Pass 15: Host Stock passive necrophage.
# Every year on each planet with Host Stock pops, up to 3 pops may auto-
# convert into Xenomorphs without requiring a purge order. This is the
# "increased rate of necrophaging" mechanic — Host Stock pops are physically
# used as hosts (face-hugged), producing Xenomorph offspring over time.
#
# Wired via on_yearly_pulse_planet (planet scope).
# =====================================================================
planet_event = {
	id = xenocivic.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = owner
		owner = {
			is_xenomorph_empire = yes
		}
		any_owned_pop_group = {
			has_slavery_type = { type = slavery_xenomorph_host_stock }
		}
	}

	immediate = {
		# Guaranteed conversion: 1 Host Stock pop per planet per year.
		weighted_random_owned_pop_group = {
			limit = {
				has_slavery_type = { type = slavery_xenomorph_host_stock }
				pop_group_size >= 1
			}
			save_event_target_as = xeno_host_stock_pop_for_class
			kill_single_pop = yes
		}

		if = {
			limit = { exists = event_target:xeno_host_stock_pop_for_class }
			event_target:xeno_host_stock_pop_for_class = {
				xeno_record_hostline_assimilation = yes
			}
			# Reuse the hybrid-or-base species picker.
			save_event_target_as = xeno_host_pop_for_class
			xeno_pick_conversion_species = yes
			create_pop = {
				species = event_target:xeno_conversion_target_species
				ethos = owner
			}
		}

		# Bonus conversion 1: 50% chance for a second pop.
		random = {
			chance = 50
			if = {
				limit = {
				any_owned_pop_group = {
					has_slavery_type = { type = slavery_xenomorph_host_stock }
					pop_group_size >= 1
				}
				}
			weighted_random_owned_pop_group = {
				limit = {
					has_slavery_type = { type = slavery_xenomorph_host_stock }
					pop_group_size >= 1
				}
				save_event_target_as = xeno_host_pop_for_class
				kill_single_pop = yes
			}
			if = {
				limit = { exists = event_target:xeno_host_pop_for_class }
				event_target:xeno_host_pop_for_class = {
					xeno_record_hostline_assimilation = yes
				}
				xeno_pick_conversion_species = yes
				create_pop = {
					species = event_target:xeno_conversion_target_species
					ethos = owner
				}
			}
		}

		# Bonus conversion 2: 25% chance for a third pop.
		random = {
			chance = 25
			if = {
				limit = {
				any_owned_pop_group = {
					has_slavery_type = { type = slavery_xenomorph_host_stock }
					pop_group_size >= 1
				}
				}
			weighted_random_owned_pop_group = {
				limit = {
					has_slavery_type = { type = slavery_xenomorph_host_stock }
					pop_group_size >= 1
				}
				save_event_target_as = xeno_host_pop_for_class
				kill_single_pop = yes
			}
			if = {
				limit = { exists = event_target:xeno_host_pop_for_class }
				event_target:xeno_host_pop_for_class = {
					xeno_record_hostline_assimilation = yes
				}
				xeno_pick_conversion_species = yes
				create_pop = {
					species = event_target:xeno_conversion_target_species
					ethos = owner
				}
			}
		}
	}
			}
			}
}
